package search;

import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.codec.binary.Base64;
import org.apache.lucene.document.Document;
import search.lucene.Query;

/**
 *
 * @author cosmin
 */
@WebServlet(name = "ViewServlet", urlPatterns = {"/view"})
public class ViewServlet extends HttpServlet {

    /**
     * Processes requests for both HTTP
     * <code>GET</code> and
     * <code>POST</code> methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        try {
            String q = request.getParameter("q");
            if (null != q) {
                String url = new String(Base64.decodeBase64(q));
                Query query = new Query();
                Document document = query.getByUrl(url);
                if (null != document) {
                    out.println("<!DOCTYPE html>");
                    out.println("<html>");
                    out.println("    <head>");
                    out.println("        <meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\">");
                    out.println("        <title>CASE - View - " + document.get("title") + "</title>");
                    out.println("        <link href=\"style/style.css\" rel=\"stylesheet\" type=\"text/css\" />");
                    out.println("    </head>");
                    out.println("    <body>");
                    out.println("        <h1 class=\"title\">CAr Search Engine</h1>");
                    out.println("        <div class=\"menu\">");
                    out.println("        </div>");
                    out.println("        <div id=\"result\">");
                    out.println("            <div class=\"view\">");
                    out.println("                <h1><a href=\"" + document.get("url") + "\" target=\"_blank\">" + document.get("title") + "</a></h1>");
                    out.println("                <h2>Price: " + document.get("proceEuro") + " Euro</h2>");
                    out.println("                <div class=\"address\"><b>Address:</b> " + document.get("address") + "</div>");
                    out.println("                <div class=\"date\"><b>Date:</b> " + document.get("date") + "</div>");
                    out.println("                <div class=\"description\"><b>Description:</b> " + document.get("description") + "</div>");
                    String[] caracteristicsKeys = new String[]{"Tip", "An", "Km", "Stare_tehnica", "Putere", "Clasa_de_poluare", "Culoare", "Numar_de_usi", "Carburant", "Caroserie", "Capacitate_cilindrica", "Cutie_de_viteze", "Tractiune", "Numar_de_locuri", "Versiune", "Capacitate_incarcare", "Lungime", "Latime", "Inaltime", "Numar_osii", "Emisii_CO2"};
                    String caracteristics = "";
                    for (String caracteristicsKey : caracteristicsKeys) {
                        String caracteristicsValue = document.get(caracteristicsKey);
                        if (null != caracteristicsValue && !caracteristicsValue.equalsIgnoreCase("")) {
                            caracteristics += "<li><i><u>" + this._getKeyName(caracteristicsKey) + "</u>:</i> " + caracteristicsValue + "</li>";
                        }
                    }
                    out.println("                <div class=\"caracteristics\"><b>Caracteristics:</b> <ul>" + caracteristics + "</ul></div>");
                    out.println("                <div class=\"accessories\"><b>Accessories:</b> " + document.get("accessories") + "</div>");
                    out.println("            </div>");
                    out.println("        </div>");
                    out.println("    </body>");
                    out.println("</html>");
                } else {
                    response.sendRedirect("not-found");
                }
            } else {
                response.sendRedirect("not-found");
            }
        } finally {
            out.close();
        }
    }

    protected String _getKeyName(String key) {
        return key.replace("_", " ").trim();
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP
     * <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Handles the HTTP
     * <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
